package com.lgj.mall.coupon.service.impl;

import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.lgj.mall.common.to.MemberPrice;
import com.lgj.mall.common.to.SkuReductionTo;
import com.lgj.mall.coupon.entity.MemberPriceEntity;
import com.lgj.mall.coupon.entity.SkuLadderEntity;
import com.lgj.mall.coupon.service.MemberPriceService;
import com.lgj.mall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lgj.mall.common.utils.PageUtils;
import com.lgj.mall.common.utils.Query;

import com.lgj.mall.coupon.dao.SkuFullReductionDao;
import com.lgj.mall.coupon.entity.SkuFullReductionEntity;
import com.lgj.mall.coupon.service.SkuFullReductionService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
    @Autowired
    SkuLadderService skuLadderService;

    @Autowired
    MemberPriceService memberPriceService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTo reductionTo) {
        //5.4、sku的优惠、满减等信息；gulimall_sms->sms_sku_ladder\sms_sku_full_reduction\sms_member_price
        //1.折扣:sms_sku_ladder
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(reductionTo.getSkuId());
        skuLadderEntity.setFullCount(reductionTo.getFullCount());
        skuLadderEntity.setDiscount(reductionTo.getDiscount());
        skuLadderEntity.setAddOther(reductionTo.getCountStatus());
        if(reductionTo.getFullCount() > 0){
            skuLadderService.save(skuLadderEntity);
        }




        //2、满减:sms_sku_full_reduction
        SkuFullReductionEntity reductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(reductionTo,reductionEntity);
        if(reductionEntity.getFullPrice().compareTo(new BigDecimal("0"))==1){
            this.save(reductionEntity);
        }


        //3、会员价格:sms_member_price
        List<MemberPrice> memberPrice = reductionTo.getMemberPrice();
        System.out.println("memberPrice = " + memberPrice);
        if (CollectionUtils.isEmpty(memberPrice)) {
            System.out.println("memberPrice 为空或 size=0，跳过保存。");
            return; // 添加return语句，提前退出方法
        }
        List<MemberPriceEntity> collect = memberPrice.stream().map(item -> {
            System.out.printf("正在处理 MemberPrice: id=%d, name=%s, price=%s%n",
                    item.getId(), item.getName(), item.getPrice());
            MemberPriceEntity priceEntity = new MemberPriceEntity();
            priceEntity.setSkuId(reductionTo.getSkuId());
            priceEntity.setMemberLevelId(item.getId());
            priceEntity.setMemberLevelName(item.getName());
            priceEntity.setMemberPrice(item.getPrice());
            priceEntity.setAddOther(1);
            return priceEntity;
        }).filter(item->{
//            return item.getMemberPrice().compareTo(new BigDecimal("0")) == 1;
            boolean valid = (item.getMemberPrice() != null
                    && item.getMemberPrice().compareTo(BigDecimal.ZERO) > 0);
            System.out.printf("过滤结果: memberPrice=%s 是否保留=%b%n",
                    item.getMemberPrice(), valid);
            return valid;
        }).collect(Collectors.toList());
//        memberPriceService.saveBatch(collect);
        System.out.printf("最终收集到的 collect 列表大小: %d%n", collect.size());
        System.out.println("最终 collect 内容: " + collect);
        if (!collect.isEmpty()) {
            memberPriceService.saveBatch(collect);
            System.out.println("保存成功！");
        } else {
            System.out.println("collect 为空，没有执行保存。");
        }
    }
}